/**	
 * Author : Mht
 * Desc : For 2016 RDM 
 */
package controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import lombok.extern.log4j.Log4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import service.menu.MenuService;
import service.token.TokenService;

import common.annotation.ControllerLog;

import configuration.PropertyNames;
import entity.menu.AccessToken;
import entity.menu.Menu;

/**
 * @author Mht
 * @date May 26, 2016
 */
@Log4j
@RestController()
@RequestMapping("/menu")
public class MenuController {

	@Resource
	private TokenService tokenService;

	@Resource
	private MenuService menuService;

	@Autowired
	private Environment env;

	@RequestMapping("/queryMenu")
	@ControllerLog(description = "Query Menu")
	@ResponseBody
	public void queryMenu(HttpServletRequest request,
			HttpServletResponse response) {

		menuService.assembleMenu();
	}

	@RequestMapping("/createMenu")
	@ResponseBody
	public void createMenu(HttpServletRequest request,
			HttpServletResponse response) {

		// 获取Access Token
		AccessToken token = tokenService.queryAccessTokenWithStatic(
				env.getProperty(PropertyNames.APPID),
				env.getProperty(PropertyNames.APPSECRET));

		// 通过所获取的Token创建自定义菜单
		if (null != token) {
			boolean result = menuService.createMenuWithStatic(assembleMenu(),
					token.getToken());
			if (result) {
				log.info("菜单创建成功.");
			} else {
				log.error("菜单创建失败!");
			}
		}
		try {
			menuService.testServiceAop();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			log.error("Create Menu Error : ", e);
		}
	}

	private Menu assembleMenu() {

		return menuService.assembleMenu();
	}
}
